package com.yanqu.road.logic.redis;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class RedisForbiddenWords {

    private static Logger logger = LogManager.getLogger(RedisForbiddenWords.class.getName());

    private static final String SEPARATOR = ",";
    /**
     * 设置第三方敏感词
     * @param appId
     * @param channelID
     * @param words
     */
    public static void setWords(String appId, String channelID, List<String> words){
        if (words == null || words.size() == 0) {
            return;
        }
        String key = RedisKeyDefineHelper.getForbiddenWordsListKey(appId, channelID);
        StringBuffer data = new StringBuffer();
        for (String word : words) {
            data.append(word).append(SEPARATOR);
        }
        data.deleteCharAt(data.length() - 1);
        RedisManger.getRedisUtil().setString(key, data.toString());
    }

    /**
     * 获取第三方敏感词
     * @param appId
     * @param channelID
     * @return
     */
    public static List<String> getWords(String appId, String channelID){
        String key = RedisKeyDefineHelper.getForbiddenWordsListKey(appId, channelID);
        if (!RedisManger.getRedisUtil().exists(key)) {
            return null;
        }
        List<String> words = new ArrayList<>();
        String data = RedisManger.getRedisUtil().get(key);
        for (String word : data.split(SEPARATOR)) {
            words.add(word);
        }
        return words;
    }

    /**
     * 获取第三方敏感词数量
     * @param appId
     * @param channelID
     * @return
     */
    public static long getWordsNum(String appId, String channelID){
        List<String> words = getWords(appId, channelID);
        if (words == null) {
            return 0;
        }
        return words.size();
    }
}
